﻿using Microsoft.DirectX.AudioVideoPlayback;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace csharp_movie_player
{
    public partial class Form1 : Form
    {
        public Video MyVideo;
        public int PanelWidth, PanelHeight;
        public string PlayingPosition, Duration;
        
        public Form1()
        {
            InitializeComponent();
            timer1.Stop();
            VolumeTrackBar.Minimum = 0;
            VolumeTrackBar.Maximum = 10;
            VolumeTrackBar.Value = 5;
        }

        public string CalculateTime(double Time)
        {
            string mm, ss, CalculatedTime;
            int h, m, s, T;

            Time = Math.Round(Time);
            T = Convert.ToInt32(Time);

            h = (T / 3600);
            T = T % 3600;
            m = (T / 60);
            s = T % 60;

            if (m < 10)
                mm = string.Format("0{0}", m);
            else
                mm = m.ToString();
            if (s < 10)
                ss = string.Format("0{0}", s);
            else
                ss = s.ToString();

            CalculatedTime = string.Format("{0}:{1}:{2}", h, mm, ss);

            return CalculatedTime;
        }

        public int CalculateVolume()
        {
            //Maximum Volume/Loudest value = 0, Minimum Volume/Mute value = -10000
            switch (VolumeTrackBar.Value)
            {
                case 1:
                    return -1500;
                case 2:
                    return -1000;
                case 3:
                    return -700;
                case 4:
                    return -600;
                case 5:
                    return -500;
                case 6:
                    return -400;
                case 7:
                    return -300;
                case 8:
                    return -200;
                case 9:
                    return -100;
                case 10:
                    return 0;
                default:
                    return -10000;
            }
        }

        public void StartUp()
        {
            if (MyVideo != null)
            {
                MyVideo.Dispose();
            }

            Text = openFileDialog1.SafeFileName + " - MPlayer 2.0";
            PanelWidth = VideoPanel.Width;
            PanelHeight = VideoPanel.Height;

            MyVideo = new Video(openFileDialog1.FileName);
            MyVideo.Owner = VideoPanel;
            VideoPanel.Width = PanelWidth;
            VideoPanel.Height = PanelHeight;

            SeekTrackBar.Minimum = Convert.ToInt32(MyVideo.CurrentPosition);
            SeekTrackBar.Maximum = Convert.ToInt32(MyVideo.Duration);
            SeekTrackBar.Value = 0;
            PlayPauseBtn.Text = "Play";

            Duration = CalculateTime(MyVideo.Duration);
            PlayingPosition = "0:00:00";
            TimeTextBox.Text = PlayingPosition + "/" + Duration;


            MyVideo.Audio.Volume = CalculateVolume();

            //To show the first frame of the video to recognize it
            MyVideo.Play();
            MyVideo.Pause();

        }

        private void OpenFileBtn_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select video file..";
            openFileDialog1.InitialDirectory = Application.StartupPath;
            openFileDialog1.DefaultExt = ".avi";
            openFileDialog1.Filter = "AVI files|*.avi|DAT files|*.dat|All files|*.*";
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            StartUp();
        }

        private void PlayPauseBtn_Click(object sender, EventArgs e)
        {
            if (MyVideo != null)
            {
                if (MyVideo.Playing)
                {
                    MyVideo.Pause();
                    timer1.Stop();
                    PlayPauseBtn.Text = "Play";
                }
                else
                {
                    MyVideo.Play();
                    timer1.Start();
                    PlayPauseBtn.Text = "Pause";
                }
            }
        }

        private void StopBtn_Click(object sender, EventArgs e)
        {
            if (MyVideo != null)
            {
                MyVideo.StopWhenReady();
                timer1.Stop();
                StartUp();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SeekTrackBar.Value = Convert.ToInt32(MyVideo.CurrentPosition);
            PlayingPosition = CalculateTime(MyVideo.CurrentPosition);
            TimeTextBox.Text = PlayingPosition + "/" + Duration;

            if (PlayingPosition == Duration)
            {
                timer1.Stop();
                StartUp();
            }
        }

        private void SeekTrackBar_Scroll(object sender, EventArgs e)
        {
            if (MyVideo != null)
            {
                MyVideo.CurrentPosition = SeekTrackBar.Value;
            }
            else
            {
                SeekTrackBar.Value = 0;
            }
        }

        private void VolumeTrackBar_Scroll(object sender, EventArgs e)
        {
            if (MyVideo != null)
            {
                MyVideo.Audio.Volume = CalculateVolume();
            }
        }
    }
}
